import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_learn_vi/src/page/train/train_teacher_course_item_widget3.dart';
import 'package:get/get.dart';

import '../../bean/bean_train.dart';
import '../common/common_loading_dialog.dart';
import '../common/common_nodata_widget.dart';
import '../common/location_page.dart';
import 'controller/eacher_work_train_details_controller.dart';

///教师的培训 详情
class TeacherWorkTrainDetailsPage extends StatefulWidget {
  final String tag;

  final int trainId;

  const TeacherWorkTrainDetailsPage(
      {Key key, @required this.tag, @required this.trainId})
      : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _TeacherWorkTrainDetailsPageState();
  }
}

class _TeacherWorkTrainDetailsPageState
    extends State<TeacherWorkTrainDetailsPage>
    with SingleTickerProviderStateMixin {
  TeacherTrainDetailsController _historyController;
  TabController _tabController;

  @override
  void initState() {
    super.initState();
    _historyController = Get.put(
      TeacherTrainDetailsController(trainId: widget.trainId),
      tag: widget.tag,
    );
    _tabController = new TabController(length: 2, vsync: this);

    Future.delayed(Duration.zero, () async {
      await _historyController.requestDetailsFunction();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("我的培训"),
      ),
      body: GetBuilder(
        id: "root",
        tag: widget.tag,
        builder: (TeacherTrainDetailsController controller) {
          return RefreshIndicator(
            notificationPredicate: (ScrollNotification notifation) {
              //该属性包含当前ViewPort及滚动位置等信息
              return true;
            },
            onRefresh: () async {
              await _historyController.requestDetailsFunction(click: true);
            },
            child: NestedScrollView(
              body: Container(
                padding: EdgeInsets.only(top: 10),
                child: buildListView(),
              ),
              headerSliverBuilder:
                  (BuildContext context, bool innerBoxIsScrolled) {
                return [
                  SliverToBoxAdapter(
                    child:
                        HomeLocationWidget(pageTag: widget.trainId.toString()),
                  )
                ];
              },
            ),
          );
        },
      ),
    );
  }

  Widget buildListView() {
    bool loading = _historyController.getIsLoading();
    if (loading) {
      return Center(
        child: LoadingWidget(),
      );
    }
    TrainBean _trainBean = _historyController.getTrainBean();
    if (_trainBean == null) {
      return Center(child: NoDataWidget());
    }
    int count = 0;
    if (_trainBean.courseList != null) {
      count = _trainBean.courseList.length;
    }
    if (_trainBean.videoCourseEntityList != null) {
      count = count + _trainBean.videoCourseEntityList.length;
    }

    return ListView.builder(
      padding: EdgeInsets.zero,
      physics: BouncingScrollPhysics(),
      shrinkWrap: true,
      itemCount: count,
      itemBuilder: (BuildContext context, int index) {
        CourseAndTeacherBean cubean;
        cubean = _trainBean.courseList[index];
        return TrainTeacherCourseItemWidget3(
          cubean: cubean,
          index: index,
        );
      },
    );
  }
}
